#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    long long maxWater(vector<int>& arr)
    {
        int left = 0, right = arr.size() - 1;
        int Min = min(arr[left], arr[right]);
        long long count = 0;
        while (left < right)
        {
            if (arr[left] <= Min)
            {
                //cerr<<"left="<<left<<endl;
                //cerr<<"Min="<<Min<<",Min-arr[left]="<<Min-arr[left]<<endl;
                count += (Min - arr[left]);
                //cerr<<"count="<<count<<endl;
                ++left;
                if (arr[left] > Min)
                {
                    Min = min(arr[left], arr[right]);
                }
            }
            else if (arr[right] <= Min)
            {
                //cerr<<"right="<<right<<endl;
                //cerr<<"Min="<<Min<<",arr[right]="<<arr[right]<<endl;
                count += (Min - arr[right]);
                //err<<"count="<<count<<endl;
                --right;
                if (arr[right] > Min)
                {
                    Min = min(arr[left], arr[right]);
                }
            }
        }
        return count;
    }
};